package com.ewzj.bigdata.mvp.ui.activity

import android.animation.IntEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.RelativeLayout
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.dpToPx
import com.ewzj.bigdata.app.extensions.initwebview
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.di.component.DaggerHourseWebActivityComponent
import com.ewzj.bigdata.di.module.HourseWebActivityModule
import com.ewzj.bigdata.mvp.contract.HourseWebActivityContract
import com.ewzj.bigdata.mvp.model.rspbean.HomeBean
import com.ewzj.bigdata.mvp.presenter.HourseWebActivityPresenter
import com.ewzj.bigdata.mvp.ui.view.ObservableScrollViewCallbacks
import com.ewzj.bigdata.mvp.ui.view.ScrollState
import com.jakewharton.rxbinding2.view.RxView
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.umeng.socialize.bean.SHARE_MEDIA
import kotlinx.android.synthetic.main.activity_home_web.*
import kotlinx.android.synthetic.main.item_webview_comment.*
import kotlinx.android.synthetic.main.path_loading.*

class HomeWebActivity : BaseActivity<HourseWebActivityPresenter>(),HourseWebActivityContract.View {
    val shareWindow by lazy { Apputils.Utils.getShareWindow(obserwebview) }
    override fun reSethint(title: String) {
        et_comment.setText("")
        et_comment.hint=title
    }

    val home by lazy { intent.getParcelableExtra<HomeBean>(AppKey.EXTRA_DATA) }
    override fun reloadUrl(url: String) {
        obserwebview.loadUrl(url)
    }

    override fun isCollected(iscollected: Int) {
       iv_star.isSelected=iscollected==1
    }

    override fun reSetTitle(title: String) {
       tv_title.text=title
    }

    override fun initData(savedInstanceState: Bundle?) {
        obserwebview.initwebview(rl_pathloading)
        initListener()
        loadWeb()
    }

    private fun loadWeb() {
        if (home==null){
            finish()
        }
        reSetTitle("来源•" + home.getSources())
        mPresenter.initBridgeWebview(obserwebview,home.three_url)
        mPresenter.addScanRecord(home.go_url)
        Apputils.Utils.setPathandStart(rl_pathloading)
    }

    private fun initListener() {
        obserwebview.setScrollViewCallbacks(object : ObservableScrollViewCallbacks{
            override fun onScrollChanged(scrollY: Int, firstScroll: Boolean, dragging: Boolean) {

            }

            override fun onDownMotionEvent() {

            }

            override fun onUpOrCancelMotionEvent(scrollState: ScrollState?) {
                if (scrollState==ScrollState.DOWN){
                    if (ll_title.getHeight() == 0) {
                        rl_partent.setPadding(0, 46.dpToPx(this@HomeWebActivity), 0, 0)
                        showSerachBar(ll_title, 0, 46.dpToPx(this@HomeWebActivity))
                    }
                }else if (scrollState==ScrollState.UP){
                    if (ll_title.getHeight() != 0) {
                        rl_partent.setPadding(0, 0, 0, 0)
                        showSerachBar(ll_title, 46.dpToPx(this@HomeWebActivity), 0)
                    }
                }
            }

        })
        RxView.clicks(tv_send).subscribe({mPresenter.doComment(et_comment.text.toString(),home)})
        RxView.clicks(iv_back).subscribe({finish()})
        RxView.clicks(tv_share).subscribe({
            shareWindow.showAtLocation(llcoment, Gravity.BOTTOM, 0, 0)
            Apputils.Utils.setbackgroudAlpa(this, 0.6.toFloat())})
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_qq)).subscribe({
            mPresenter.ShareWeb(home, SHARE_MEDIA.QQ)
            shareWindow.dismiss()
        })
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_qqzone)).subscribe({
            mPresenter.ShareWeb(home, SHARE_MEDIA.QZONE)
            shareWindow.dismiss()
        })
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_wechat)).subscribe({
            mPresenter.ShareWeb(home, SHARE_MEDIA.WEIXIN)
            shareWindow.dismiss()
        })
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_wechatzone)).subscribe({
            mPresenter.ShareWeb(home, SHARE_MEDIA.WEIXIN_CIRCLE)
            shareWindow.dismiss()
        })
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_sina)).subscribe({
            mPresenter.ShareWeb(home, SHARE_MEDIA.SINA)
            shareWindow.dismiss()
        })
    }

    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_home_web
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerHourseWebActivityComponent.builder().appComponent(appComponent)
                .hourseWebActivityModule(HourseWebActivityModule(this))
                .build()
                .inject(this)
    }

    override fun showLoading() {

    }

    override fun launchActivity(intent: Intent?) {

    }

    override fun hideLoading() {

    }

    override fun killMyself() {

    }

    override fun showMessage(message: String?) {

    }
    companion object{
        fun startHomeActivity(context: Context,homeBean: HomeBean){
            var intent=Intent(context,HomeWebActivity::class.java)
            intent.putExtra(AppKey.EXTRA_DATA,homeBean)
            context.startActivity(intent)
        }
    }

    fun showSerachBar(view: View, startValue: Int, endValue: Int) {
        //      RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) rlSearch.getLayoutParams();
        val valueAnimator = ValueAnimator.ofInt(0, 100)
        valueAnimator.duration = 200
        valueAnimator.addUpdateListener { animation ->
            val animatorValue = animation.animatedValue as Int
            val fraction = animatorValue / 100f
            val mEvaluator = IntEvaluator()
            //3.使用IntEvaluator计算属性值并赋值给ListView的高
            val layoutParams = view.layoutParams as RelativeLayout.LayoutParams
            layoutParams.height = mEvaluator.evaluate(fraction, startValue, endValue)!!
            view.layoutParams = layoutParams
            view.requestLayout()
        }
        valueAnimator.setTarget(view)
        valueAnimator.start()
    }
}
